package com.nguc.ngucpractice.controller;

import com.nguc.ngucpractice.common.controller.BaseController;
import com.nguc.ngucpractice.pojo.Product;
import com.nguc.ngucpractice.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * User:曹帅
 * Date:2020/8/2
 * Version:1.0
 */
@RestController
@RequestMapping("/manage/product")
public class ProductController extends BaseController {

    @Autowired
    private ProductService productService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView findAll(
            @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pageSize", defaultValue = "3") Integer pageSize
    ) {
        return feedback(productService.findProducts(pageNum, pageSize));
    }

    @PostMapping(value = "/updateStatus")
    public ModelAndView updateStatus(@RequestBody Map<String, Integer> map) throws Exception {
        return feedback(productService.updateStatus(map.get("productId"), map.get("status")));
    }

    @PostMapping(value = "/add")
    public ModelAndView addProduct(@RequestBody Product product) throws Exception {
        return feedback(productService.addProduct(product));
    }

    @PostMapping(value = "/update")
    public ModelAndView updateProduct(@RequestBody Product product) throws Exception {
        return feedback(productService.updateProduct(product));
    }

}
